<?php
include("../sys/Kernel.amv.php");
if(isset($_REQUEST['user']) AND isset($_REQUEST['pass']))
{
	if(!empty($_REQUEST['user']) AND !empty($_REQUEST['pass']))
	{
		$user = $_REQUEST['user'];
		$pass = $_REQUEST['pass'];
		$passHash = sha1($pass);
		$connect = $bdd->query("SELECT * FROM utilisateur WHERE user = '".$user."'");
		$rep = $connect->fetch();
		if($rep['pass'] == $passHash)
		{
			$sess = sha1(rand()."user".$pass);
			$sesstime = time() + (60 * 60 * 24 * 7);
			$bdd->query("UPDATE utilisateur SET sess = '".$sess."', sesstime = '".$sesstime."'");
			$_SESSION["user"] = $user;
			$_SESSION["sess"] = $sess;
			setcookie("user", $user, $sesstime);
			setcookie("sess", $sess, $sesstime);
			echo json_encode(array("CONNECTED",$user,$sess));//echo "CONNECTED";
		}
		else
		{
			echo json_encode(array("BAD_AUTH","",""));
		}
	}
	else
	{
		echo json_encode(array("PARAM_EMPTY","",""));
	}
}
else
{
	echo json_encode(array("PARAM_MISSING","",""));
}

?>